library(tidyverse)
library(ggthemes)
library(gridExtra)
library(RColorBrewer)
library(plotly)
salary <- read_csv("/Users/Dan/Research/TidyTuesday/tidy_tuesday_week2.csv")
gath <- salary %>%
  gather( pos, salary, -year) %>%
  group_by(pos, year) %>%
  top_n(16, salary) %>%
  filter(salary < 30000000) %>%
  mutate(salary=salary/1000000) 

# Order Position Labels
gath$pos <- factor(gath$pos, levels = c("Quarterback", "Running Back", "Wide Receiver", "Tight End", "Offensive Lineman", "Cornerback", "Safety", "Linebacker", "Defensive Lineman", "Special Teamer"))

# Assigning Positions to Offense or D/ST
gath$side <- ifelse(gath$pos == "Quarterback" | gath$pos == "Running Back" | gath$pos == "Wide Receiver" | gath$pos == "Tight End" | gath$pos == "Offensive Lineman" ,"Offense", "D/ST")
gath <- na.omit(gath)

# Order Labels Again
gath$side <- factor(gath$side, levels = c("Offense", "D/ST"))


# Expanding Sprectral Color Palette
coul = brewer.pal(8,"Spectral") 
coul = colorRampPalette(coul)(10)
coul <- sample(coul)


# Theme Customizing
facetSettings <-
  theme(panel.grid.major = element_line("lightgray",0.5),
        panel.grid.minor = element_line("lightgray",0.25))



k <- ggplot(gath, aes(x=year, y=salary, color = pos)) + 
  geom_smooth(method = "loess", se=F,  size = 3, span = 0.3) +
  #geom_jitter(alpha=0.1, size = 4, width=0.15) +
  theme_minimal() +
  scale_colour_manual(values = coul) +
  ylab("Cap Hit ($ million)") +
  xlab("Year") +
  theme(legend.position="bottom") +
  labs(title = "NFL Salaries by Position Over Time") +
  theme(plot.title = element_text(size=20, face="bold")) +
  theme(title = element_text(vjust=2)) +
  geom_hline(yintercept=0)

k <- k + facet_wrap(~ side, ncol=5) + facetSettings + theme(strip.text.x = element_text(size = 18))

ggplotly(k) %>%
  layout(legend = list(orientation = "v"))